﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Create.Master" Inherits="System.Web.Mvc.ViewPage<Langben.DAL.ts_check>" %>

<%@ Import Namespace="Common" %>
<%@ Import Namespace="Models" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="CurentPlace" runat="server">
      创建 库存盘点
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <fieldset>
        <legend>
          <%--  <input class="a2 f2" type="button"  value="创建" onclick="save()"/>--%>
            <input class="a2 f2" type="button" onclick="BackList('ts_check')" value="返回" />
        </legend>
        <div class="bigdiv">
            <%: Html.HiddenFor(model => model.i_sign)%>
            <%: Html.HiddenFor(model => model.GoodsDetail)%> 
            <%: Html.HiddenFor(model => model.i_no)%> 
            <%: Html.HiddenFor(model => model.c_clerk)%>
            <input type="hidden" id="tbPerson" value="<%: Html.Encode(ViewData["person"])%>" />
            <input type="hidden" id="tbID" />
            <table style="width:100%">
            <tr>
            <td><%: Html.LabelFor(model => model.i_no) %>：</td>
            <td><input type="text" id="tbi_no" value="<%: Html.Encode(ViewData["DH"])%>" /></td>
            <td> <%: Html.LabelFor(model => model.dt_dt) %>：</td>
            <td><%: Html.TextBox("dt_dt", ((Model != null && Model.dt_dt != null) ? Model.dt_dt.ToString().AsDateTime().ToString("yyyy-MM-dd") : ""), new { style = "width: 214px;", @class="easyui-datebox" })%>
                <%: Html.ValidationMessageFor(model => model.dt_dt) %></td>
            <td> <%: Html.LabelFor(model => model.c_base) %>：</td>
            <td><%=Html.DropDownListFor(model => model.c_base, Models.SysFieldModels.GetBaseInf(), "请选择")%>
                <%: Html.ValidationMessageFor(model => model.c_base) %></td></tr>
            <tr>
            <td> <%: Html.LabelFor(model => model.c_agency) %>：</td>

            <td><%: Html.EditorFor(model => model.c_agency) %>
                <%: Html.ValidationMessageFor(model => model.c_agency) %></td>
                <td><%: Html.LabelFor(model => model.c_clerk) %>：</td>
            <td><input type="text" id="tbc_clerk" /></td>
            <td><%: Html.LabelFor(model => model.c_keeper) %>：</td>
            <td><%: Html.EditorFor(model => model.c_keeper) %>
                <%: Html.ValidationMessageFor(model => model.c_keeper) %></td> 
            </tr><tr>
            <td> <%: Html.LabelFor(model => model.c_comment) %>：</td>
            <td colspan="5"><%: Html.EditorFor(model => model.c_comment) %>
                <%: Html.ValidationMessageFor(model => model.c_comment) %></td></tr>
            <tr><td  colspan="6">
             <fieldset style="width:90%">
                <table><tr><td>分类：</td>
                <td><%=Html.DropDownListFor(model => model.GoodesClass, Models.SysFieldModels.GetGoodsClass(), "请选择")%></td></tr></table>
             </fieldset>
            </td></tr></table>
         
          
        </div>
    </fieldset>
      <div style="float:left;"> 
          <div style="width:880px; height:350px"><table id="flexigridData"></table></div>  
      </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    
    <script type="text/javascript">

        $(function () {
            DoGrid();

            $('#i_sign').val("盘点中");
            $('#c_clerk').val($('#tbPerson').val());
            $('#tbc_clerk').val($('#tbPerson').val());
            $('#tbc_clerk').attr("disabled", true);
            $('#i_no').val($('#tbi_no').val());
            $('#tbi_no').attr("disabled", true);

            $('span[class="l-btn-text icon-search l-btn-icon-left"]').attr("disabled", true);
            $('#flexigridGoodsData').datagrid({
                // title: '物品目录', //列表的标题
                iconCls: 'icon-site',
                width: 'auto',
                height: 'auto',
                nowrap: false,
                striped: true,
                collapsible: true,
                url: '../ts_goods/GetData', //获取数据的url         ?c_class='+$('#GoodesClass').val()
                sortName: 'Id',
                sortOrder: 'desc',
                idField: 'Id',

                toolbar: [
                     {
                         text: '选择',
                         iconCls: 'icon-ok',
                         handler: function () {
                             return flexiSelect();
                         }
                     }],
                columns: [[
                      { field: 'i_sign', title: '状态', width: 60 }
                	, { field: 'c_code', title: '编号', width: 80 }
				 	, { field: 'c_desc', title: '物品', width: 80 }
					, { field: 'c_model', title: '规格', width: 80 }
					, { field: 'c_unit', title: '单位', width: 80 }
					, { field: 'c_class', title: '类别', width: 80 }
					, { field: 'c_class1', title: '大类别', width: 52, hidden: true }
					, { field: 'f_pricelast', title: '最后进价', width: 52, hidden: true }
					, { field: 'f_upper', title: '库存上限', width: 60 }
					, { field: 'f_lower', title: '库存下限', width: 60 }
					, { field: 'c_py', title: '拼音', width: 52, hidden: true }
					, { field: 'c_comment', title: '备注', width: 52, hidden: true }
					, { field: 'c_vendor', title: '主供应商', width: 90 }
					, { field: 'c_base', title: '专用仓库', width: 90 }
                ]],
                pagination: true,
                rownumbers: true

            }); 
        });

        //返回
        function BackList(url) {

            window.location.href = '../../' + url;
        }

        function save() {
            var rows = $('#flexigridData').datagrid('getRows');
            var count = 0;
            var allrow = "";
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].Id == "" || rows[i].Id == null || rows[i].Id.toString() == "undefined") {

                    var row = JSON.stringify(rows[i]);
                    allrow = allrow + row + "^";
                }
            }

            $('#GoodsDetail').val(allrow);
            alert($('#GoodsDetail').val());
            $.ajax({
                url: this.action,
                type: "Post",
                data: $(document.forms[0]).serialize(),
                dataType: "json",
                success: function (data) {
                    if ($.messager) {
                        $.messager.defaults.ok = '继续';
                        $.messager.defaults.cancel = '返回';

                        $.messager.confirm('操作提示', data, function (r) {
                            if (!r) {
                                window.location.href = 'javascript:history.go(-1)';
                            }
                        });
                    }

                }
            });


        }




        function DoGrid() {
            $('#flexigridData').datagrid({
                iconCls: ' ',
                width: 'auto',
                height: 'auto',
                nowrap: false,
                striped: true,
                fit: true,
                collapsible: true, 
                sortName: 'Id',
                sortOrder: 'desc',
                idField: 'Id',

                toolbar: [

                   {
                       text: '建盘点表',
                       iconCls: 'icon-add',
                       handler: function () {
                           return CreatePDB();
                       }
                   }, {
                       text: '保存数据',
                       iconCls: 'icon-edit',
                       id: 'saveData',
                       handler: function () {
                           return SaveData();
                       }

                   }, { 
                       text: '生成损溢单',
                       iconCls: 'icon-search',
                       handler: function () {
                           return ExchangeSYD();
                       } 
                       //                   }, {
                       //                       text: '输入盘点数',
                       //                       iconCls: 'icon-search',
                       //                       handler: function () {
                       //                           return insert();
                       //                       }
                   }, {
                       text: '退出',
                       iconCls: 'icon-loss',
                       handler: function () {
                           BackList('ts_check');
                       }
                   }],
                columns: [[
					  { field: 'c_code', title: '编号', width: 50 }
					, { field: 'c_desc', title: '品名', width: 120 }
					, { field: 'c_model', title: '规格', width: 205 }
                    , { field: 'c_unit', title: '单位', width: 65 }
                    , { field: 'f_quant', title: '库存数量', width: 80, editor: 'text' }
                    , { field: 'f_quant_check', title: '盘点数量', width: 80, editor: 'text' }
                    , { field: 'SYCount', title: '损溢数量', width: 80 }
                    , { field: 'c_comment', title: '备注', width: 40, editor: 'text' }
                    , { field: 'action', title: '操作', width: 100, align: 'center',
                        formatter: function (value, row, index) { 
                            var editrow = $('#flexigridData').datagrid('getRows');
                            var row1 = JSON.stringify(editrow[index]);
                            var row2 = JSON.parse(row1);
            //                alert(row2.Id);
                            if (row2.Id == undefined) {
                                if (row.editing) {
                                    var s = '<a href="#" onclick="saverow(' + index + ')">保存</a> ';
                                    var c = '<a href="#" onclick="cancelrow(' + index + ')">取消</a>';
                                    return s + c;
                                } else {
                                    var e = '<a href="#" onclick="editrow(' + index + ')">编辑</a> ';
                                    var d = '<a href="#" onclick="deleterow(' + index + ')">删除</a>';
                                    return e + d;
                                }
                            }
                        }
                    }
                ]],
                onBeforeEdit: function (index, row) {
                    row.editing = true;
                    updateActions();
                },
                onAfterEdit: function (index, row) {
                    row.editing = false;
                    updateActions();
                },
                onCancelEdit: function (index, row) {
                    row.editing = false;
                    updateActions();
                },

                pagination: true,
                rownumbers: false

            });

        }

        //不可编辑
        function DoGrid2() {
            $('#flexigridData').datagrid({
                iconCls: ' ',
                width: 'auto',
                height: 'auto',
                nowrap: false,
                striped: true,
                fit: true,
                collapsible: true,
                sortName: 'Id',
                sortOrder: 'desc',
                idField: 'Id',

                toolbar: [

                   {
                       text: '建盘点表',
                       iconCls: 'icon-add',
                       handler: function () {
                           return CreatePDB();
                       }
                   }, {
                       text: '保存数据',
                       iconCls: 'icon-edit',
                       id: 'saveData',
                       handler: function () {
                           return SaveData();
                       }

                   }, {
                       text: '生成损溢单',
                       iconCls: 'icon-search',
                       handler: function () {
                           return ExchangeSYD();
                       }
                       //                   }, {
                       //                       text: '输入盘点数',
                       //                       iconCls: 'icon-search',
                       //                       handler: function () {
                       //                           return insert();
                       //                       }
                   }, {
                       text: '退出',
                       iconCls: 'icon-loss',
                       handler: function () {
                           BackList('ts_check');
                       }
                   }],
                columns: [[
					  { field: 'c_code', title: '编号', width: 50 }
					, { field: 'c_desc', title: '品名', width: 120 }
					, { field: 'c_model', title: '规格', width: 205 }
                    , { field: 'c_unit', title: '单位', width: 65 }
                    , { field: 'f_quant', title: '库存数量', width: 80, editor: 'text' }
                    , { field: 'f_quant_check', title: '盘点数量', width: 80, editor: 'text' }
                    , { field: 'SYCount', title: '损溢数量', width: 80 }
                    , { field: 'c_comment', title: '备注', width: 40, editor: 'text' }
                    , { field: 'action', title: '操作', width: 100, align: 'center'}
                ]],
                onBeforeEdit: function (index, row) {
                    row.editing = true;
                    updateActions();
                },
                onAfterEdit: function (index, row) {
                    row.editing = false;
                    updateActions();
                },
                onCancelEdit: function (index, row) {
                    row.editing = false;
                    updateActions();
                },

                pagination: true,
                rownumbers: false

            });

        }



        //建盘点菜
        function CreatePDB() {
            if ($('#c_base').val() == "") {
                alert("请选择要盘点的仓库");
                return;
            }
            var rows = $('#flexigridData').datagrid('getRows');
            if (rows.length > 0) {

                if (confirm("当前盘点表已有数据，是否要清除当前数据，重建盘点表？")) {
                   var rowLength=rows.length;
                   for (var i = 0; i < rowLength; i++) { 
                        $('#flexigridData').datagrid('deleteRow', 0);
                    } 
                }
                else {
                    return;
                }
            }

            if (confirm("盘点前请不要做入库、出库操作，否则盘点数据将不准！按“确定”开始键盘点单，按“取消”返回")) {

                $.post("../ts_goods/GetDataByClass", { className: $('#GoodesClass').val() }, function (result) {
                    if (result.success) {
                        for (var i = 0; i < result.data.length; i++) {
                            indexs = i;
                            $('#flexigridData').datagrid('insertRow', {
                                index: indexs,
                                row: {
                                    c_code: result.data[i].c_code,
                                    c_desc: result.data[i].c_desc,
                                    c_model: result.data[i].c_model,
                                    c_unit: result.data[i].c_unit,
                                    f_quant:0,
                                    f_quant_check:0,
                                    SYCount:0,
                                    c_comment:""
                                }
                            });
                             
            
                        }
                    }

                });
            }
            else {
                return;
            }
           
            $('#flexigridData').datagrid('selectRow', 0);
            $('#flexigridData').datagrid('beginEdit', 0);
            $('#flexigridData').datagrid('selectRow', 1);
            $('#flexigridData').datagrid('beginEdit', 1);
            $('#flexigridData').datagrid('selectRow', 2);
            $('#flexigridData').datagrid('beginEdit', 2);

        }

        //生成损溢单
        function ExchangeSYD() { 
            if ($('#tbID').val() == "") {
                return;
            }
            if (confirm("确定要将盘点表生成损溢单？生成的损溢单在审核过帐后才能改变库存数量")) {
                var rows = $('#flexigridData').datagrid('getRows'); 
                var allrow = "";
                for (var i = 0; i < rows.length; i++) {
                    if (rows[i].Id == "" || rows[i].Id == null || rows[i].Id.toString() == "undefined") {

                        var row = JSON.stringify(rows[i]);
                        allrow = allrow + row + "^";
                    }
                }

                $('#GoodsDetail').val(allrow);

                $.post("../../ts_check/CheckIfSY", { search: $('#GoodsDetail').val() }, function (res) {
                    if (res.result) {
                        if (confirm("该盘点表中商品没有一个发生损溢，不需要生成盘点单，是否结束改盘点表，按“确定”结束该盘点表，按“取消”重新输入盘点数量")) {
                            $('span[class="l-btn-text icon-add l-btn-icon-left"]').attr("disabled", true);
                            $('span[class="l-btn-text icon-edit l-btn-icon-left"]').attr("disabled", true);
                            $('span[class="l-btn-text icon-search l-btn-icon-left"]').attr("disabled", true);
                        }
                        else {
                            $.post("../../ts_check/ChangeState", { search: $('#tbID').val(), sign: "已结束" }, function (result) {

                                if (result.result) {
                                    DoGrid2();
                                    $("#flexigridData").datagrid("reload");
                                }
                            });
                        }
                    }
                    else {
                        $.post("../../ts_check/ChangeState", { search: $('#tbID').val(), sign: "已结束" }, function (result) {

                            if (result.result) {
                                DoGrid2();
                                $("#flexigridData").datagrid("reload");
                            }
                        });
                    }

                }); 
            }
         }

        //保存数据
        function SaveData() {
            var rows = $('#flexigridData').datagrid('getRows');

            var count = 0;
            var allrow = "";
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].Id == "" || rows[i].Id == null || rows[i].Id == undefined) {

                    var row = JSON.stringify(rows[i]);
                    allrow = allrow + row + "^";
                }
            }

            $('#GoodsDetail').val(allrow);
            if (rows.length > 0) { 
                if ($('span[class="l-btn-text icon-search l-btn-icon-left"]').attr("disabled") == "disabled") {
                    if (confirm("是否确定要保存？")) {
                        $.ajax({
                            url: this.action,
                            type: "Post",
                            data: $(document.forms[0]).serialize(),
                            dataType: "json",
                            success: function (res) { 
                                $('#tbID').val(res.Id); 
                                alert("保存成功！");
                            }
                        });
                    }
                    else {
                        return;
                    }
                }
                //            $('span[class="l-btn-text icon-add l-btn-icon-left"]').attr("disabled", "false");

                $('span[class="l-btn-text icon-search l-btn-icon-left"]').attr("disabled", false); 
            }
            else {
                alert("盘点表没有内容！");
            }
 
        }

        function updateActions() {
            var rowcount = $('#flexigridData').datagrid('getRows').length;
            for (var i = 0; i < rowcount; i++) {
                $('#flexigridData').datagrid('updateRow', {
                    index: i,
                    row: { action: '' }
                });
            }
        }

        function saverow(index) {

            $('#flexigridData').datagrid('endEdit', index);


        }


        function editrow(index) {
            $('#flexigridData').datagrid('beginEdit', index);
        }

        function deleterow(index) {
            $.messager.confirm('操作提示', '确定删除吗?', function (r) {
                if (r) {
                    $('#flexigridData').datagrid('deleteRow', index);
                }
            });
        }

        function cancelrow(index) {
            $('#flexigridData').datagrid('cancelEdit', index);
        }

        function insert() { 
            $('#divQuery').dialog("open");
        }


    </script>
</asp:Content>

